package com.seis_645.appTesting;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DealerActivity extends ListActivity {

	private DealerDataSource datasource;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dealer_main);
		final ListView lv = getListView();
		datasource = new DealerDataSource(this);
		datasource.open();
		List<Dealer> values = datasource.get_all_dealers();
		ArrayAdapter<Dealer> adapter = new ArrayAdapter<Dealer>(this,
				android.R.layout.simple_list_item_1, values);

		lv.setAdapter(adapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> a, View v, int position, long id) {
				Dealer dealer = (Dealer) lv.getItemAtPosition(position);
				goToDealer(dealer);
			}
		});

	}
	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}
	
	@Override 
	protected void onDestroy() {
		datasource.close();
		super.onDestroy();
	}

	public void onClick(View view) {
		TabGroupActivity parentActivity = (TabGroupActivity) getParent();
		switch (view.getId()) {
		case R.id.btn_add_dealer:
			Intent new_dealer = new Intent(getParent(),
					SaveDealerActivity.class);
			parentActivity.startChildActivity("SaveDealerActivity", new_dealer);
			break;
		case R.id.btn_compare_dealer:
			Intent compareDealer = new Intent(getParent(),
					DealerCompareActivity.class);
			parentActivity.startChildActivity("DealerCompareActivity",compareDealer);	
		}
		return;
	}
	
	public void goToDealer(Dealer dealer) {
		TabGroupActivity parentActivity = (TabGroupActivity) getParent();
		Intent dealerInfo = new Intent(getParent(), DealerInfoActivity.class);
		dealerInfo.putExtra("dealer_id",dealer.get_id());
		parentActivity.startChildActivity("DealerInfoActivity",dealerInfo);
		return;
	}

}
